{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Make some noise"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "Reachy 2 has two microphones located on each antenna and one speaker inside its torso. The audio API allows you to record and play audio files.\n",
    "\n",
    "***Note that, for now, the audio files are located in a temporary folder on Reachy's computer and are deleted at each reboot of the robot. The file management is very basic, allowing just a list of files in a single folder.***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Manage the sound files"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "First, connect to your robot.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from reachy2_sdk import ReachySDK\n",
    "\n",
    "reachy = ReachySDK(host='localhost')  # Replace with the actual IP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "You can first check the audio files available on Reachy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.get_audio_files()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "The list may be empty if you have just started to play with Reachy. Let's add a new audio file. Reachy is able to play wav, mp3, and ogg files. Ogg files can be downloaded [here for instance](https://getsamplefiles.com/sample-audio-files/ogg).\n",
    "Make sure to change the path name in the line below. For instance */home/name/Downloads/sample-1.ogg* or *C:\\Users\\name\\Downloads\\sample-1.ogg*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c068ad53",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.upload_audio_file('<path>/sample-1.ogg')\n",
    "reachy.audio.upload_audio_file('<path>/sample-4.ogg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc7eb487",
   "metadata": {},
   "source": [
    "Now these two files should be listed on Reachy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f93e5709",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.get_audio_files()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ff75b25",
   "metadata": {},
   "source": [
    "A file can be removed if you don't want to use it. It should no longer be in the list!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "076a76e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.remove_audio_file('sample-4.ogg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "823fb0fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.get_audio_files()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "### Play a sound\n",
    "\n",
    "Once an audio file is uploaded onto Reachy, you can play it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.play_audio_file('sample-1.ogg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "Stop the playback whenever you want"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.stop_playing()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f77bc1e",
   "metadata": {},
   "source": [
    "### Record a sound\n",
    "\n",
    "In a similar way, a sound can be recorded using Reachy's microphones. Only the ogg extension is allowed for now. The duration of the recording, in seconds, must be set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8379a6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.record_audio('tutorial.ogg', duration_secs=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0738588b",
   "metadata": {},
   "source": [
    "The audio is recorded in the background; it may be stopped at any time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d03a93a",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.stop_recording()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "835b2865",
   "metadata": {},
   "source": [
    "The audio file is now on Reachy's file system, and available for listening"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6038c1c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.get_audio_files()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89beb16a",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.play_audio_file('tutorial.ogg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd3afb1",
   "metadata": {},
   "source": [
    "Finally, the audio file can be downloaded to your local machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7178c172",
   "metadata": {},
   "outputs": [],
   "source": [
    "reachy.audio.download_audio_file('tutorial.ogg', '<path>')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
